﻿package mortal.game.view.forging.view
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.rotate3dModelSprite.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.forging.util.*;
    import mortal.game.view.palyer.*;
    import mortal.game.view.playerSystem.view.*;
    import mortal.game.view.wizard.panel.*;
    import mortal.mvc.core.*;

    public class EquipDisplaySpr extends GSprite
    {
        protected var _window:Window;
        protected var _equipPanel:ForgingEquipsPanel;
        protected var _leftBtn:GLoadedButton;
        protected var _rightBtn:GLoadedButton;
        protected var _comBat:BitmapNumberText;
        protected var _playerTargetPanel:PlayerTargetPanel;
        protected var _shakeEffect:SWFPlayer;
        protected var _img2d:Img2D;
        protected var _roleModelPlayer:RoleModelPlayer;
        private var _roleBg:GBitmap;
        private var _rotate3d:Rotate3dModelSprite;
        private var _currSelEquip:PlayerEquipItem;
        protected var _frameTimer:FrameTimer;
        protected var _turnValue:int;
        private var _defaultEquipData:ItemData;

        public function EquipDisplaySpr(param1:Window)
        {
            this._window = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_2:GBitmap = null;
            super.createDisposedChildrenImpl();
            this._rotate3d = UIFactory.getUICompoment(Rotate3dModelSprite, 53 + 10, 116, this);
            this._rotate3d.setSize(165, 273);
            this._comBat = UIFactory.bitmapNumberText(153 + 55 - 7, 52 - 4, "GuildCopyNum.png", 24, 32, -6, this, 9, BitmapNumberText.Mid);
            var _loc_1:int = 0;
            while (_loc_1 < 4)
            {
                
                _loc_2 = UIFactory.gBitmap(ImagesConst.PackItemBg, 60 + 8 + 4 + 45 * _loc_1, 35 + 46, this);
                this.pushUIToDisposeVec(_loc_2);
                _loc_1++;
            }
            this._playerTargetPanel = UICompomentPool.getUICompoment(PlayerTargetPanel);
            this._playerTargetPanel.x = 60 + 8 + 3 + 2;
            this._playerTargetPanel.y = 35 + 47;
            this.addChild(this._playerTargetPanel);
            this._equipPanel = UICompomentPool.getUICompoment(ForgingEquipsPanel);
            this._equipPanel.x = 28 + 3;
            this._equipPanel.y = 10 + 75;
            this.addChild(this._equipPanel);
            this._equipPanel.mouseEnabled = true;
            this.mouseChildren = true;
            this._equipPanel.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._leftBtn = UIFactory.gLoadedButton(ImagesConst.TurnLeft2_upSkin, 192 + 3, 320 + 87, 40, 36, this);
            this._leftBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onClickTurnBtn);
            this._leftBtn.configEventListener(MouseEvent.MOUSE_UP, this.stopTurning);
            this._rightBtn = UIFactory.gLoadedButton(ImagesConst.TurnRight2_upSkin, 58 + 17 + 7, 320 + 87, 40, 36, this);
            this._rightBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onClickTurnBtn);
            this._rightBtn.configEventListener(MouseEvent.MOUSE_UP, this.stopTurning);
            this.configEventListener(DragEvent.Event_Move_In, this.moveInHandler);
            Dispatcher.addEventListener(EventName.ResetCurrSelEquip, this.resetCurrSelEquip);
            this._shakeEffect = UICompomentPool.getUICompoment(SWFPlayer);
            this._shakeEffect.timeRate = 3;
            this._shakeEffect.move(53 + 19 + 20 + 2, 40 + 64 - 3);
            this._shakeEffect.load("ShakeEffect.swf", ModelType.NormalSwf, null);
            this.addChild(this._shakeEffect);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.ForgingEquipTip, 20 + 44, 200 + 228, this));
            this._roleBg = UIFactory.gBitmap("");
            LoaderManager.instance.load(ImagesConst.RoleBg4 + ".swf", this.onLoadCompl);
            this.updateComBat();
            this.addListeners();
            return;
        }// end function

        private function onLoadCompl(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._roleBg.bitmapData = param1.bitmapData as BitmapData;
            this.updateUI();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.ShowGemEmbedInfo, this.onShowGemEmbedInfoHandler);
            Dispatcher.addEventListener(EventName.ForgingSelDefaultEquip, this.onSelDefaultEquipHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemUpdateHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.ShowGemEmbedInfo, this.onShowGemEmbedInfoHandler);
            Dispatcher.removeEventListener(EventName.ForgingSelDefaultEquip, this.onSelDefaultEquipHandler);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemUpdateHandler);
            return;
        }// end function

        public function updateUI() : void
        {
            this.add3DRole();
            return;
        }// end function

        protected function onClickTurnBtn(event:MouseEvent) : void
        {
            if (this._roleModelPlayer)
            {
                if (event.currentTarget == this._leftBtn)
                {
                    this._turnValue = 2;
                }
                if (event.currentTarget == this._rightBtn)
                {
                    this._turnValue = -2;
                }
                this.start();
            }
            return;
        }// end function

        protected function stopTurning(event:MouseEvent = null) : void
        {
            if (this._frameTimer)
            {
                this._frameTimer.stop();
            }
            return;
        }// end function

        protected function onTurning(param1:FrameTimer) : void
        {
            if (this._roleModelPlayer)
            {
                this._roleModelPlayer.rotationY = this._roleModelPlayer.rotationY + this._turnValue;
            }
            return;
        }// end function

        private function start() : void
        {
            if (!this._frameTimer)
            {
                this._frameTimer = new FrameTimer(1, int.MAX_VALUE, true);
                this._frameTimer.addListener(TimerType.ENTERFRAME, this.onTurning);
            }
            this._frameTimer.start();
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as PlayerEquipItem;
            this.switchEquip(_loc_2);
            return;
        }// end function

        private function switchEquip(param1:PlayerEquipItem) : void
        {
            var _loc_3:StrengthenPanel = null;
            var _loc_2:* = GameController.forging.forgingWin;
            if (param1 && param1.itemData)
            {
                if (param1 != this._currSelEquip)
                {
                    if (!this.isCanForging(param1.itemData))
                    {
                        return;
                    }
                    if (this._currSelEquip)
                    {
                        this._currSelEquip.isSelect = false;
                        if (_loc_2.currentPanel is StrengthenPanel)
                        {
                            _loc_3 = _loc_2.currentPanel as StrengthenPanel;
                            if (_loc_3.isPlaying1 || _loc_3.isPlaying2)
                            {
                                _loc_3.stopAllStrengthenEffect();
                            }
                        }
                    }
                    this._currSelEquip = param1;
                    param1.isSelect = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AddForgingEquip, param1));
                }
            }
            return;
        }// end function

        private function isCanForging(param1:ItemData) : Boolean
        {
            var _loc_2:* = GameController.forging.forgingWin;
            if (_loc_2 == null)
            {
                return true;
            }
            if (_loc_2.currSelPage == ForgingConst.Page_EquipRefresh)
            {
                if (!EquipmentUtil.isCanRefresh(param1))
                {
                    MsgManager.showRollTipsMsg(Language.getString(41117));
                    return false;
                }
            }
            if (_loc_2.currSelPage == ForgingConst.Page_ColorAdvance)
            {
                if (!EquipmentUtil.isCanColorAdvance(param1))
                {
                    MsgManager.showRollTipsMsg(Language.getString(41118));
                    return false;
                }
            }
            if (_loc_2.currSelPage == ForgingConst.Page_QualityUpgrade)
            {
                if (!EquipmentUtil.isCanStarAdvance(param1))
                {
                    MsgManager.showRollTipsMsg(StarAdvanceUtil.getFailAdvanceDesc(param1), MsgRollTipsType.msgRollTips3);
                    return false;
                }
            }
            return true;
        }// end function

        private function onSelDefaultEquipHandler(event:DataEvent) : void
        {
            var _loc_2:PlayerEquipItem = null;
            this._defaultEquipData = event.data as ItemData;
            if (this._defaultEquipData)
            {
                _loc_2 = this.getEquipById(this._defaultEquipData.uid);
                if (_loc_2)
                {
                    this.switchEquip(_loc_2);
                }
            }
            return;
        }// end function

        private function moveInHandler(event:DragEvent) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:String = null;
            var _loc_7:int = 0;
            var _loc_2:* = event.dragItem as BaseItem;
            var _loc_3:* = event.dropItem as BaseItem;
            if (_loc_2 && _loc_2.itemData.serverData.posType != EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                _loc_4 = _loc_2.itemData.itemInfo.type;
                _loc_5 = _loc_2.itemData.itemInfo.category;
                _loc_6 = "";
                _loc_7 = -1;
                if (_loc_3)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Equip, _loc_2.itemData));
                }
            }
            return;
        }// end function

        private function createSkillList() : void
        {
            return;
        }// end function

        private function init3dRect() : void
        {
            (this._window as ForgingModule).rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(11, 59 + 2, 763, 436), this._window);
            Rect3DManager.instance.windowShowHander(null, this._window);
            return;
        }// end function

        protected function add3DRole() : void
        {
            var _loc_1:* = (this._window as ForgingModule).rect3d;
            if (_loc_1 == null)
            {
                this.init3dRect();
                _loc_1 = (this._window as ForgingModule).rect3d;
            }
            if (_loc_1)
            {
                _loc_1.removeImg(this._img2d);
                this._img2d = new Img2D(null, this._roleBg.bitmapData, new Rectangle(0, 0, 270, 436), false);
                _loc_1.addImg(this._img2d);
                this._img2d.x = 492;
                this.updateRoleModel();
            }
            return;
        }// end function

        public function getAvailableEquip() : PlayerEquipItem
        {
            var _loc_1:PlayerEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                if (_loc_1.isAvailable)
                {
                    return _loc_1;
                }
            }
            return null;
        }// end function

        protected function updateRoleModel() : void
        {
            var _loc_1:* = (this._window as ForgingModule).rect3d;
            if (_loc_1)
            {
                this._roleModelPlayer = FrObjectPool.getObject(RoleModelPlayer, this);
                this._roleModelPlayer.entityInfo = Cache.instance.role.roleEntityInfo;
                this._roleModelPlayer.scaleAll = 1.4;
                this._roleModelPlayer.setRenderList(_loc_1.renderList);
                _loc_1.addObject3d(this._roleModelPlayer, 140 + 300 + 187, 330 - 40 + 72);
                this._rotate3d.model = this._roleModelPlayer;
            }
            return;
        }// end function

        public function updateComBat() : void
        {
            if (Cache.instance.role.entityInfo.combat)
            {
                this._comBat.text = Cache.instance.role.entityInfo.combat.toString();
            }
            return;
        }// end function

        public function updateAllEmbedState() : void
        {
            return;
        }// end function

        public function updateAllGemUpgradeState() : void
        {
            return;
        }// end function

        public function updateAllRefreshState() : void
        {
            var _loc_1:PlayerEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                this.setEquipRefreshState(_loc_1);
            }
            return;
        }// end function

        public function updateAllColorAdvanceState() : void
        {
            var _loc_1:PlayerEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                this.setEquipColorAdvanceState(_loc_1);
            }
            return;
        }// end function

        public function updateAllStarAdvanceState() : void
        {
            var _loc_1:PlayerEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                this.setEquipStarAdvanceState(_loc_1);
            }
            return;
        }// end function

        private function setEquipEmbedState(param1:PlayerEquipItem) : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:ItemExInfo = null;
            var _loc_4:int = 0;
            if (param1 && param1.itemData)
            {
                _loc_3 = param1.itemData.extInfo;
                if (EquipmentUtil.isCanEmbed(param1.itemData))
                {
                    _loc_4 = 1;
                    while (_loc_4 <= 8)
                    {
                        
                        if (_loc_3["h" + _loc_4] == "0")
                        {
                            _loc_2 = true;
                            break;
                        }
                        _loc_4++;
                    }
                }
                param1.isAvailable = _loc_2;
            }
            return;
        }// end function

        private function setGemUpgradeState(param1:PlayerEquipItem) : void
        {
            if (param1 && param1.itemData)
            {
                param1.isAvailable = EquipmentUtil.isCanGemUpgrade(param1.itemData);
            }
            return;
        }// end function

        private function setEquipRefreshState(param1:PlayerEquipItem) : void
        {
            if (param1 && param1.itemData)
            {
                param1.isAvailable = EquipmentUtil.isCanRefresh(param1.itemData);
            }
            return;
        }// end function

        private function setEquipColorAdvanceState(param1:PlayerEquipItem) : void
        {
            if (param1 && param1.itemData)
            {
                param1.isAvailable = EquipmentUtil.isCanColorAdvance(param1.itemData);
            }
            return;
        }// end function

        private function setEquipStarAdvanceState(param1:PlayerEquipItem) : void
        {
            if (param1 && param1.itemData)
            {
                param1.isAvailable = EquipmentUtil.isCanStarAdvance(param1.itemData);
            }
            return;
        }// end function

        public function getEquipById(param1:String) : PlayerEquipItem
        {
            var _loc_2:PlayerEquipItem = null;
            for each (_loc_2 in this._equipPanel.equipmentList)
            {
                
                if (_loc_2.itemData && _loc_2.itemData.uid == param1)
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        public function getEquipByType(param1:int) : PlayerEquipItem
        {
            return this._equipPanel.getEquipByType(param1);
        }// end function

        public function clear() : void
        {
            var _loc_1:PlayerEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                if (_loc_1)
                {
                    _loc_1.clear();
                }
            }
            this.resetCurrSelEquip();
            return;
        }// end function

        private function resetCurrSelEquip(event:DataEvent = null) : void
        {
            if (this._currSelEquip)
            {
                this._currSelEquip.isSelect = false;
                this._currSelEquip = null;
            }
            return;
        }// end function

        private function onShowGemEmbedInfoHandler(event:DataEvent) : void
        {
            var _loc_2:PlayerEquipItem = null;
            for each (_loc_2 in this._equipPanel.equipmentList)
            {
                
                _loc_2.clearStrengInfo();
                _loc_2.updateGemEmbedInfo();
            }
            return;
        }// end function

        public function showStrengInfo() : void
        {
            var _loc_1:OtherEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                _loc_1.updateStrengLevel();
            }
            return;
        }// end function

        public function get equipmentList() : Vector.<OtherEquipItem>
        {
            return this._equipPanel.equipmentList;
        }// end function

        private function remove3D() : void
        {
            var _loc_1:* = (this._window as ForgingModule).rect3d;
            if (_loc_1)
            {
                _loc_1.removeImg(this._img2d);
                _loc_1.disposeObj3d(this._roleModelPlayer);
            }
            this._img2d = null;
            this._roleModelPlayer = null;
            return;
        }// end function

        public function upDateEquipByType(param1:int, param2:EquipUpdateParamInfo) : void
        {
            this._equipPanel.updateEquipByType(param1, param2);
            return;
        }// end function

        public function upDateAllEquip() : void
        {
            this._equipPanel.updateAllEquip();
            return;
        }// end function

        public function updateEquipOperState() : void
        {
            var _loc_1:OtherEquipItem = null;
            for each (_loc_1 in this._equipPanel.equipmentList)
            {
                
                if (_loc_1 && _loc_1.itemData)
                {
                    if (GemUtil.getCanEmbedHoleInEquip(_loc_1.itemData) || GemUtil.getCanAdvanceHoleInEquip(_loc_1.itemData) || GemUtil.getCanStrengHoleInEquip(_loc_1.itemData))
                    {
                        _loc_1.isCanOperate = true;
                        continue;
                    }
                    _loc_1.isCanOperate = false;
                }
            }
            return;
        }// end function

        private function backPackItemUpdateHandler(param1:Object) : void
        {
            if ((GameController.forging.view as ForgingModule).currentPanel is GemStrengthenPanel3)
            {
                this.updateEquipOperState();
            }
            return;
        }// end function

        public function set currSelEquip(param1:PlayerEquipItem) : void
        {
            this._currSelEquip = param1;
            return;
        }// end function

        public function get currSelEquip() : PlayerEquipItem
        {
            return this._currSelEquip;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._equipPanel.dispose(param1);
            this._leftBtn.dispose(param1);
            this._rightBtn.dispose(param1);
            this._comBat.dispose(param1);
            this._roleBg.dispose(param1);
            this._rotate3d.dispose(param1);
            this._playerTargetPanel.dispose(param1);
            this._shakeEffect.dispose();
            this._equipPanel = null;
            this._leftBtn = null;
            this._rightBtn = null;
            this._comBat = null;
            this._roleBg = null;
            this._defaultEquipData = null;
            this._rotate3d = null;
            this._playerTargetPanel = null;
            this._shakeEffect = null;
            this._frameTimer = null;
            this._turnValue = 0;
            this.stopTurning();
            this.remove3D();
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.stopTurning);
            this.removeListeners();
            return;
        }// end function

    }
}
